# -*- coding: UTF-8 -*-

from abaqus import *
from abaqusConstants import *
import regionToolset

def tiquUF(odb_path,data_name):
    odb = session.openOdb(name=odb_path, readOnly=False) #filename

    session.XYDataFromHistory(name='ALLAE Whole Model-1', odb=odb, 
        outputVariableName='Artificial strain energy: ALLAE for Whole Model', 
        steps=('Step-1', ), __linkedVpName__='Viewport: 1')
    session.XYDataFromHistory(name='ALLIE Whole Model-1', odb=odb, 
        outputVariableName='Internal energy: ALLIE for Whole Model', steps=(
        'Step-1', ), __linkedVpName__='Viewport: 1')
    session.XYDataFromHistory(name='RF2 PI: RIGIDPLATE-1 N: 1 NSET RF-TOPPLATE-1', 
        odb=odb, 
        outputVariableName='Reaction force: RF2 PI: RIGIDPLATE-1 Node 1 in NSET RF-TOPPLATE', 
        steps=('Step-1', ), __linkedVpName__='Viewport: 1')
    session.XYDataFromHistory(name='U2 PI: RIGIDPLATE-1 N: 1 NSET RF-TOPPLATE-1', 
        odb=odb, 
        outputVariableName='Spatial displacement: U2 PI: RIGIDPLATE-1 Node 1 in NSET RF-TOPPLATE', 
        steps=('Step-1', ), __linkedVpName__='Viewport: 1')
        
    xy1 = session.xyDataObjects['U2 PI: RIGIDPLATE-1 N: 1 NSET RF-TOPPLATE-1']
    xy2 = session.xyDataObjects['RF2 PI: RIGIDPLATE-1 N: 1 NSET RF-TOPPLATE-1']
    xy3 = combine(abs(xy1), abs(xy2))
    xy3.setValues(
        sourceDescription='combine ( abs ( "U2 PI: RIGIDPLATE-1 N: 1 NSET RF-TOPPLATE-1" ),abs ( "RF2 PI: RIGIDPLATE-1 N: 1 NSET RF-TOPPLATE-1" ) )')
    tmpName = xy3.name
    UF_name = data_name + '-UF'
    session.xyDataObjects.changeKey(tmpName, UF_name) #filename 

    xy1 = session.xyDataObjects[UF_name] #filename
    xy2 = butterworthFilter(xyData=xy1, cutoffFrequency=0.2)
    xy2.setValues(
        sourceDescription='butterworthFilter ( xyData=' + UF_name + ' , cutoffFrequency= 0.2 )')
    tmpName = xy2.name
    session.xyDataObjects.changeKey(tmpName, UF_name + '-filter02') #filename

    xy1 = session.xyDataObjects['ALLAE Whole Model-1']
    xy2 = session.xyDataObjects['ALLIE Whole Model-1']
    xy3 = xy1/xy2
    xy3.setValues(sourceDescription='"ALLAE Whole Model-1"/"ALLIE Whole Model-1"')
    tmpName = xy3.name
    session.xyDataObjects.changeKey(tmpName, data_name + '-ALLAE') #filename
